using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using ZoneFiveSoftware.Common.Data.Fitness;
using System.Drawing;

namespace FilteredStatistics.Common.Controller
{
    class ProxyStatisticsProvider : IStatisticsProvider
    {
        public ProxyStatisticsProvider(object provider)
        {
            if (provider == null)
            {
                throw new Exception("Invalid provider pointer");
            }

            Type providerType = provider.GetType();

            m_ProviderObject = provider;

            m_ColumnIdsProperty = providerType.GetProperty("ColumnIds", typeof(IList<Guid>));
            m_GetHeaderMethod = providerType.GetMethod("GetColumnHeader", new Type[] { typeof(Guid), typeof(IActivity) });
            m_GetColorMethod = providerType.GetMethod("GetColumnColor", new Type[] { typeof(Guid) });
            m_GetLabelMethod = providerType.GetMethod("GetStatisticLabel", new Type[] { typeof(Guid), typeof(object) });
            m_GetTotalLabelMethod = providerType.GetMethod("GetTotalStatisticLabel", new Type[] { typeof(Guid), typeof(IList<object>) });
            m_CompareRangesMethod = providerType.GetMethod("CompareRanges", new Type[] { typeof(Guid), typeof(int), typeof(object), typeof(object) });

            if (m_ColumnIdsProperty == null ||
                !m_ColumnIdsProperty.CanRead ||
                m_GetHeaderMethod == null ||
                m_GetHeaderMethod.ReturnType != typeof(String) ||
                m_GetColorMethod == null ||
                m_GetColorMethod.ReturnType != typeof(Color) ||
                m_GetLabelMethod == null ||
                m_GetLabelMethod.ReturnType != typeof(String) ||
                m_GetTotalLabelMethod == null ||
                m_GetTotalLabelMethod.ReturnType != typeof(String) ||
                m_CompareRangesMethod == null ||
                m_CompareRangesMethod.ReturnType != typeof(int))
            {
                throw new Exception("Provider doesn't implement IStatisticsProvider interface");
            }
        }

#region IStatisticsProvider Members

        public IList<Guid> ColumnIds
        {
            get { return m_ColumnIdsProperty.GetValue(m_ProviderObject, null) as IList<Guid>; }
        }

        public String GetColumnHeader(Guid columnId, IActivity activity)
        {
            return m_GetHeaderMethod.Invoke(m_ProviderObject, new object[] { columnId, activity }) as String;
        }

        public Color GetColumnColor(Guid columnId)
        {
            return (Color)m_GetColorMethod.Invoke(m_ProviderObject, new object[] { columnId });
        }

        public String GetStatisticLabel(Guid columnId, object currentRange)
        {
            return m_GetLabelMethod.Invoke(m_ProviderObject, new object[] { columnId, currentRange }) as String;
        }

        public String GetTotalStatisticLabel(Guid columnId, IList<object> allRanges)
        {
            return m_GetTotalLabelMethod.Invoke(m_ProviderObject, new object[] { columnId, allRanges }) as String;
        }

        public int CompareRanges(Guid columnId, int sortingOrder, object range1, object range2)
        {
            return (int)m_CompareRangesMethod.Invoke(m_ProviderObject, new object[] { columnId, sortingOrder, range1, range2 });
        }

#endregion

        private object m_ProviderObject = null;
        private PropertyInfo m_ColumnIdsProperty = null;
        private MethodInfo m_GetHeaderMethod = null;
        private MethodInfo m_GetColorMethod = null;
        private MethodInfo m_GetLabelMethod = null;
        private MethodInfo m_GetTotalLabelMethod = null;
        private MethodInfo m_CompareRangesMethod = null;
    }
}
